package com.hmy.uaa.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmy.uaa.bo.request.RoleResourceRequest;
import com.hmy.uaa.mapper.RoleResourceRelationMapper;
import com.hmy.uaa.po.RoleResourceRelation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class RoleResourceRelationService {


    @Autowired
    private RoleResourceRelationMapper roleResourceRelationMapper;


    /**
     * 根据角色id删除资源
     *
     * @param roleId 角色id
     */
    public void delete(Long roleId) {
        LambdaQueryWrapper<RoleResourceRelation> queryWrapper = Wrappers.<RoleResourceRelation>lambdaQuery()
                .eq(RoleResourceRelation::getRoleId, roleId);
        roleResourceRelationMapper.delete(queryWrapper);
    }

    /**
     * 批量新增角色资源数据
     *
     * @param roleResourceRequest 请求参数
     */
    @Transactional
    public void insertBatch(RoleResourceRequest roleResourceRequest) {

        RoleResourceRelation roleResourceRelation;
        for (Long resourceId : roleResourceRequest.getResourceIds()) {
            roleResourceRelation = new RoleResourceRelation();
            roleResourceRelation.setRoleId(roleResourceRequest.getRoleId());
            roleResourceRelation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(roleResourceRelation);
        }
    }
}
